/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RoomPanel.MatchRoom;

import Store.Tactics;
import players.PlayersEnum;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ivanhing
 */
public class MatchRoomPanel extends HttpServlet {

    private final String connectionString = "jdbc:sqlserver://w2ksa.cs.cityu.edu.hk:1433;databaseName=aiad048_db;";
    private final String conACPW = "aiad048";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
//        String P1 = request.getParameter("Player1");
//        String P2 = request.getParameter("Player2");
//        int PID1 = Integer.parseInt(P1);
//        int PID2 = Integer.parseInt(P2);
        String PID1 = "000000";
        String PID2 = "000000";
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MatchRoomPanel</title>");
        out.println("<a>Servlet MatchRoomPanel</a>");
        out.println("</head>");
        out.println("<body>");

        out.println("<style type=\"text/css\">");
        out.println("body{background-image:url('http://db.tt/HJgFAj3U') repeat fixed;}");
        out.println("</style>");


        out.println("<h1>MY Team</h1>");

        out.println("<script> ");
        out.println("<!-- ");

        out.println(" var milisec=0 ");
        out.println(" var seconds=600 ");
        out.println(" document.counter.d2.value='600' ");
        out.println("function show_alert(){window.location=\"match.html\"}");

        out.println(" function display(){ ");
        out.println(" if (milisec<=0){ ");
        out.println("    milisec=9 ");
        out.println("    seconds-=1  } ");
        out.println(" if (seconds<=-1){ ");
        out.println("    milisec=0 ");
        out.println("    seconds+=1 ");
        out.println(" } ");
        out.println(" else ");
        out.println("    milisec-=1 ");
        out.println("    document.counter.d2.value=seconds +\".\"+milisec ");
        out.println("    setTimeout(\"display()\",100) }");

        out.println(" --> ");
        out.println(" </script> ");
        out.println("<input type=\"button\" id=\"player2-rm2\" class=\"button\" value=\"Start Match\" onclick=\"show_alert()\"><br>");
        out.println("<a>Remaining Time:</a><form name=\"counter\"><input type=\"text\" size=\"8\" name=\"d2\" value=\"Rest\"></form>");


        try {
            /*
             * TODO output your page here. You may use following sample code.
             */

            Connection con = DriverManager.getConnection(connectionString, conACPW, conACPW);
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Statement stmt = con.createStatement();
            String strSQL = "SELECT name FROM Customers_Players INNER JOIN player ON Customers_Players.playerID = player.playerID where Customers_Players.UID='000000'";
            ResultSet rsT = stmt.executeQuery(strSQL);
            //int numRow = countRow(rsT);
            out.println("<a>Players:</a>");
            out.println("<table style=\"width:500PX\"><tr>");

            while (rsT != null && rsT.next() != false) {
                String Pname = rsT.getString("name"); //tactics name 
                out.println("<td>&nbsp;<img src='" + PlayersEnum.getPlayerImageURLByName(Pname)
                        + "' width=\"100px\" height=\"110px\"><br><a href=\"#\" onclick=\"abc.style.background=\'#f3f3f3 url("
                        + PlayersEnum.getCardURLByName(Pname) + ") no-repeat\';\"><br>" + Pname + "<br>FOR DETAIL</a></td>");
            }
            out.println("</tr></table><ul class=\"abc\" id=\"abc\" style=\" width:600px; height:800px\"></ul>");
         out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(MatchRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MatchRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(MatchRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MatchRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
